// 块级数学表达式
const mathExtension = {
    name: 'math',
    level: 'inline',                                     // Is this a block-level or inline-level tokenizer?
    start(src) { return src.match(/^\$+/)?.index; }, // Hint to Marked.js to stop and check for a match
    tokenizer(src, tokens) {
        const rule = /^\$+(.+?)\$+/;    // Regex for the complete token, anchor to string start
        const match = rule.exec(src);
        if (match) {
            const token = {                                 // Token to generate
                type: 'math',                      // Should match "name" above
                raw: match[0],                                // Text to consume from the source
                text: match[1].trim(),                        // Additional custom properties
                tokens: []                                    // Array where child inline tokens will be generated
            };
            //this.lexer.inline(src.substring(token.raw.length), token.tokens);    // Queue this data to be processed for inline tokens
            return token;
        }
    },
    renderer(token) {
        //return token.raw; // 这里不渲染了，因为渲染后不自定加载字体，导致不显示，所以这里只相当于不对文本内容进行处理，后续统一处理
        return MathJax.tex2chtml(token.text).outerHTML;// + this.parser.parseInline(token.tokens); // parseInline to turn child tokens into HTML
    }
};

// 内联数学表达式，不管是一个$还是两个$$都识别
const mathInlineExtension = {
    name: 'math_inline',
    level: 'inline',                                     // Is this a block-level or inline-level tokenizer?
    start(src) { return src.match(/^.*?[^\\]\$/)?.index; }, // Hint to Marked.js to stop and check for a match
    tokenizer(src, tokens) {
        const rule = /^.*?[^\\\$](\$+[^\$]+?\$+)/;    // Regex for the complete token, anchor to string start
        const match = rule.exec(src);
        if (match) {
            var raw = match[0];
            var text2 = match[1];
            var text = text2.match(/\$+([^\$]+?)\$+/)[1].trim();
            var pre = match[0].substring(0, match[0].indexOf(text2));
            const token = {                                 // Token to generate
                type: 'math_inline',                      // Should match "name" above
                pre: pre,
                raw: raw,                                // Text to consume from the source
                text: text,                        // Additional custom properties
                text2: text2,
                tokens: []                                    // Array where child inline tokens will be generated
            };
            //this.lexer.inline(src.substring(token.raw.length), token.tokens);    // Queue this data to be processed for inline tokens
            return token;
        }
    },
    renderer(token) {
        console.log(token);
        var display = token.pre.length && token.pre[token.pre.length-1] == "\n";
        return marked.parseInline(token.pre) + MathJax.tex2chtml(token.text, {display: display}).outerHTML;//token.raw.substring(index-1); // 这里不渲染了，因为渲染后不自定加载字体，导致不显示，所以这里只相当于不对文本内容进行处理，后续统一处理
        //return MathJax.tex2chtml(token.text).outerHTML;// + this.parser.parseInline(token.tokens); // parseInline to turn child tokens into HTML
    }
};



marked.use({ extensions: [mathInlineExtension, mathExtension] });

